package com.laguille.games.tetris.model.Tetromino.impl;

import com.laguille.games.tetris.model.Field;
import com.laguille.games.tetris.model.Tetromino.Coordinates;
import com.laguille.games.tetris.model.Tetromino.Position;
import com.laguille.games.tetris.model.Tetromino.TetrominoBlock;
import com.laguille.games.tetris.model.Tetromino.TetrominoPiece;

public class JPiece extends TetrominoPiece {

	//											block[3]		              
	// Blocks constitution up:   				block[2]   block[1]   block[0]

	// Blocks constitution left:   				           block[0]
//														   block[1]
//												block[3]   block[2]
	
	// Blocks constitution down:   				block[0]   block[1]   block[2]
//												   					  block[3]
	
	
//	   Blocks constitution right:							block[2]   block[3]
//															block[1]
//							   							    block[0]
	
	public JPiece()
	{
		super();
		blocks[0] = new TetrominoBlock(new Coordinates((Field.WIDTH / 2) + 1, Field.HEIGHT - 2));
		blocks[1] = new TetrominoBlock(new Coordinates((Field.WIDTH / 2), Field.HEIGHT - 2));
		blocks[2] = new TetrominoBlock(new Coordinates((Field.WIDTH / 2) - 1, Field.HEIGHT - 2));
		blocks[3] = new TetrominoBlock(new Coordinates((Field.WIDTH / 2) - 1, Field.HEIGHT - 1));
		this.position = Position.UP;
	}
	
	public JPiece(Position position, Coordinates...coordinates)
	{
		super(coordinates);
		this.position = position;
	}

	@Override
	public TetrominoPiece rotateLeft()
	{
		JPiece piece = new JPiece(position, blocks[0].getCoordinates(), blocks[1].getCoordinates(), blocks[2].getCoordinates(), blocks[3].getCoordinates());
		switch (position)
		{
			case UP:
				piece.blocks[0].setCoordinates(new Coordinates(blocks[0].getCoordinates().getX() - 1, blocks[0].getCoordinates().getY() + 1));
				piece.blocks[2].setCoordinates(new Coordinates(blocks[2].getCoordinates().getX() + 1, blocks[2].getCoordinates().getY() - 1));
				piece.blocks[3].setCoordinates(new Coordinates(blocks[3].getCoordinates().getX(), blocks[3].getCoordinates().getY() - 2));
				piece.position = Position.LEFT;
				break;
			case LEFT:
				piece.blocks[0].setCoordinates(new Coordinates(blocks[0].getCoordinates().getX() - 1, blocks[0].getCoordinates().getY() - 1));
				piece.blocks[2].setCoordinates(new Coordinates(blocks[2].getCoordinates().getX() + 1, blocks[2].getCoordinates().getY() + 1));
				piece.blocks[3].setCoordinates(new Coordinates(blocks[3].getCoordinates().getX() + 2, blocks[3].getCoordinates().getY()));
				piece.position = Position.DOWN;
				break;
			case DOWN:
				piece.blocks[0].setCoordinates(new Coordinates(blocks[0].getCoordinates().getX() + 1, blocks[0].getCoordinates().getY() - 1));
				piece.blocks[2].setCoordinates(new Coordinates(blocks[2].getCoordinates().getX() - 1, blocks[2].getCoordinates().getY() + 1));
				piece.blocks[3].setCoordinates(new Coordinates(blocks[3].getCoordinates().getX(), blocks[3].getCoordinates().getY() + 2));
				piece.position = Position.RIGHT;
				break;
			case RIGHT:
				piece.blocks[0].setCoordinates(new Coordinates(blocks[0].getCoordinates().getX() + 1, blocks[0].getCoordinates().getY() + 1));
				piece.blocks[2].setCoordinates(new Coordinates(blocks[2].getCoordinates().getX() - 1, blocks[2].getCoordinates().getY() - 1));
				piece.blocks[3].setCoordinates(new Coordinates(blocks[3].getCoordinates().getX() - 2, blocks[3].getCoordinates().getY()));
				piece.position = Position.RIGHT;
				break;
		}
		return piece;
	}

	@Override
	public TetrominoPiece rotateRight() {
		JPiece piece = new JPiece(position, blocks[0].getCoordinates(), blocks[1].getCoordinates(), blocks[2].getCoordinates(), blocks[3].getCoordinates());
		switch (position)
		{
			case UP:
				piece.blocks[0].setCoordinates(new Coordinates(blocks[0].getCoordinates().getX() - 1, blocks[0].getCoordinates().getY() - 1));
				piece.blocks[2].setCoordinates(new Coordinates(blocks[2].getCoordinates().getX() + 1, blocks[2].getCoordinates().getY() + 1));
				piece.blocks[3].setCoordinates(new Coordinates(blocks[3].getCoordinates().getX() + 2, blocks[3].getCoordinates().getY()));
				piece.position = Position.RIGHT;
				break;
			case LEFT:
				piece.blocks[0].setCoordinates(new Coordinates(blocks[0].getCoordinates().getX() + 1, blocks[0].getCoordinates().getY() - 1));
				piece.blocks[2].setCoordinates(new Coordinates(blocks[2].getCoordinates().getX() - 1, blocks[2].getCoordinates().getY() + 1));
				piece.blocks[3].setCoordinates(new Coordinates(blocks[3].getCoordinates().getX(), blocks[3].getCoordinates().getY() + 2));
				piece.position = Position.UP;
				break;
			case DOWN:
				piece.blocks[0].setCoordinates(new Coordinates(blocks[0].getCoordinates().getX() + 1, blocks[0].getCoordinates().getY() + 1));
				piece.blocks[2].setCoordinates(new Coordinates(blocks[2].getCoordinates().getX() - 1, blocks[2].getCoordinates().getY() - 1));
				piece.blocks[3].setCoordinates(new Coordinates(blocks[3].getCoordinates().getX() - 2, blocks[3].getCoordinates().getY()));
				piece.position = Position.LEFT;
				break;
			case RIGHT:
				piece.blocks[0].setCoordinates(new Coordinates(blocks[0].getCoordinates().getX() - 1, blocks[0].getCoordinates().getY() + 1));
				piece.blocks[2].setCoordinates(new Coordinates(blocks[2].getCoordinates().getX() + 1, blocks[2].getCoordinates().getY() - 1));
				piece.blocks[3].setCoordinates(new Coordinates(blocks[3].getCoordinates().getX(), blocks[3].getCoordinates().getY() - 2));
				piece.position = Position.DOWN;
				break;
		}
		return piece;
	}
	

}
